//
//  RZController.m
//  macIRIS
//
//  Created by Riziq Sayegh on 03/11/08.
//  Copyright 2008 McMaster University. All rights reserved.
//

#import "RZController.h"
#define USER_ID @"b3badd8ca9b38fe555f20a79209eea83"
#define USER_URL @"http://www.psychology.mcmaster.ca/iris"

@implementation RZController


- (IBAction)fetchDatabase:(id)sender
{
	NSString *urlString = [NSString stringWithFormat: @"%@/index.php?p=feed&code=%@", USER_URL, USER_ID];
	NSURL *url = [NSURL URLWithString:urlString];
	
	NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];	
	NSData *urlData;
	NSURLResponse *response;
	NSError *error;
	urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
	if (!urlData) {
		NSAlert *alert = [NSAlert alertWithError:error];
		[alert runModal];
		return;
	}
	
	[doc release];
	doc = [[NSXMLDocument alloc] initWithData:urlData options:0 error:&error];
	NSLog(@"doc = %@", doc);
	if (!doc) {
		NSAlert *alert = [NSAlert alertWithError:error];
		[alert runModal];
		return;
	}
	
	[itemNodes release];
	itemNodes = [[doc nodesForXPath:@"iris/item" error:&error] retain];
	NSLog(@"itemnode = %@", itemNodes);
	if (!itemNodes) {
		NSAlert *alert = [NSAlert alertWithError:error];
		[alert runModal];
		return;
	}
	
	//Update the interface
	[tableView reloadData];
	
}
				  
- (IBAction)findAndReplace:(id)sender
{
	NSDictionary* errorDict;
	NSAppleEventDescriptor* returnDescriptor = NULL;
	
	NSString* appleScriptString = [[NSString alloc] initWithFormat:
						@"\
						tell application \"Microsoft Word\"\n\
						set selFind to find object of selection\n\
						set forward of selFind to true\n\
						set wrap of selFind to find continue\n\
						set content of selFind to \"%@\"\n\
						set content of replacement of selFind to \"%@\"\n\
						execute find selFind replace replace all\n\
						end tell", [find stringValue], [replace stringValue]];
	
	NSAppleScript* scriptObject = [[NSAppleScript alloc] initWithSource: appleScriptString];
	returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
	[scriptObject release];
	[appleScriptString release];
}

- (NSString *)stringForPath:(NSString *)xp ofNode:(NSXMLNode *)n
{
	NSError *error;
	NSArray *nodes = [n nodesForXPath:xp error:&error];
	if (!nodes) {
		NSAlert *alert = [NSAlert alertWithError:error];
		[alert runModal];
		return nil;
	}
	if ([nodes count] == 0) {
		return nil;
	} else {
		return [[nodes objectAtIndex:0] stringValue];
	}
}

#pragma mark TableView data source methods

- (int)numberOfRowsInTableView:(NSTableView*)tv
{
	return [itemNodes count];
}
- (id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row
{
	NSXMLNode *node = [itemNodes objectAtIndex:row];
	NSString *xPath = [tableColumn identifier];
	return [self stringForPath:xPath ofNode:node];

}

- (void)awakeFromNib
{
	[tableView setDoubleAction:@selector(openItem:)];
	[tableView setTarget:self];
}

- (void)openItem:(id)sender
{
	int row = [tableView clickedRow];
	if (row == -1) {
		return;
	}
	NSXMLNode *clickedItem = [itemNodes objectAtIndex:row];
	NSString *entryKey = [self stringForPath:@"entrykey" ofNode:clickedItem];
	
	NSDictionary* errorDict;
	NSAppleEventDescriptor* returnDescriptor = NULL;
	
	NSString* appleScriptString = [[NSString alloc] initWithFormat:
								   @"\
								   tell application \"Microsoft Word\"\n\
								   type text selection text \"{%@} \"\n\
								   end tell", entryKey];
	
	NSAppleScript* scriptObject = [[NSAppleScript alloc] initWithSource: appleScriptString];
	returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
	[scriptObject release];
	[appleScriptString release];
}

@end
